<?php

declare(strict_types=1);

namespace app\controller\admin;

use app\model\admin\CateModel;
use think\facade\Validate;
use think\Request;

class CateController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        //
        $param  = $request->param(['created_at', 'name', 'status']);
        $list = CateModel::scope('where', $param)->where('pid', null)->with(['cate' => function ($q) {
            $q->order('id', 'desc');
        }])->order('id', 'desc')->paginate([
            'list_rows' => $this->limit,
            'page' => $this->page,
            'var_page' => $this->pageVar
        ]);
        return successPage($list);
    }
    public function getAll(Request $request)
    {
        //获取所有的一级
        $type = input('type', 1);
        if ($type == 1) {
            $data = CateModel::where('pid', null)->select();
        } else {
            $data = CateModel::where('pid', '<>', null)->select();
        }
        return success($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        //
        $validate = Validate::rule([
            "name" => "require",
            "sort" => "require",
            // "pid" => "require",
            // "pic" => "require",
            "status" => "require",
            // "status" => "require",
        ]);
        $param = $request->param(['name', "sort", 'pid', 'pic', 'status']);
        // 检验参数
        if (!$validate->check($param)) {
            return error($validate->getError());
        }
        $res = CateModel::create($param);
        if ($res) {
            return success();
        } else {
            return error("添加失败");
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $validate = Validate::rule([
            "name" => "require",
            "sort" => "require",
            // "pid" => "require",
            // "pic" => "require",
            "status" => "require",
            // "id" => "require",
            // "status" => "require",
        ]);
        $param = $request->param(['name', "sort", 'pid', 'pic', 'status']);
        // 检验参数
        if (!$validate->check($param)) {
            return error($validate->getError());
        }
        if (!$param['pid']) {
            $param['pid'] = null;
        }
        $res = CateModel::update($param, ["id" => $id]);
        if ($res) {
            return success();
        } else {
            return error("修改失败");
        }
    }
    public function changeStatus(Request $request, $id)
    {
        // 修改状态
        $info = CateModel::find($id);
        if ($info) {
            if ($info->status == 1) {
                $info->status = 2;
            } else {
                $info->status = 1;
            }
            $info->save();
            return success();
        } else {
            return error("参数错误");
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        if (!$id) {
            return error('参数错误');
        }
        $info = CateModel::find($id);
        if (count($info->cate)) {
            return error('请先删除子分类');
        }
        $res = CateModel::destroy($id);
        return success();
    }
}
